<?php
class UserModel extends Model {	
	/**
	 * 
	 * 检测用户是否登录
	 * @return boolean
	 */
	public function hasSignin() {
		return session('_LOGIN_') ? true : false;
	}
	
	/**
	 * 
	 * 处理用户登录
	 * @param array $data
	 */
	public function signin($data = array()) {

		if(!(isset($data['username']) && isset($data['password']))) {
		
			  return false;
		}

		$option['username'] = $data['username'];
		$option['password'] = $this->hashPassword($data['password']);
		$user = M('User')->where(" username = '".$option['username']."' and password = '".$option['password']."'")->find();

		if(empty($user)) 
		{
			return false;
		}else{
		        $rolelist =M("role_user")->query("select ru.role_id,r.name,r.alias from cm_role_user   ru 
		                                          LEFT JOIN ".C('DB_PREFIX')."role r  ON ru.role_id = r.id WHERE ru.user_id = ".$user['id']);

				$user['rolelist'] = $rolelist;  
				//设置登录有效活跃时间
				session('_LOGIN_',$user);
				return true;		  
          
		}

	}
	
	/**
	 * 
	 * 处理用户退出
	 */
	public function logOut() {

			session('[destroy]'); // 销毁session
			setcookie('TRACK', '', time() - 3600, '/');
	}
	
	
	/**
	 * 
	 * 加密密码
	 * @param string $password 密码
	 */
	public function hashPassword($password) {
		return md5($password);
	}
    

	#获取系统用户列表

    public function getUserList($map = '',  $field = '*', $order = 'uid ASC', $limit = 30) {
    	$res  = M('user')->where($map)->field($field)->order($order)->findPage($limit);
    	return $res;
     }

     
	#获取系统角色列表

    public function getRoleList() {

    	$list =M("Role")->where('')->order('id DESC')->limit()->select();
    	return $list;
     }


	#获取系统资源列表

    public function getResourceList() {

    	$list =M("Resource")->where('')->order('id DESC')->limit()->select();
    	return $list;
     }
     

}
?>